package com.yhf.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhf.mapper.PMapper;
import com.yhf.model.Project;
import com.yhf.dto.ProjectOv;
import com.yhf.service.PService;
import com.yhf.utils.PageResult;
import com.yhf.utils.QueryPageBean;
import com.yhf.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    PService pService;
    @Autowired
    PMapper pMapper;


    @GetMapping("findAll")
    public List<Project> findAll(){
        return pService.list();
    }

    @RequestMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        QueryWrapper<ProjectOv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        Page<ProjectOv> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
//        Page<Project> page1 = pService.page(page);
        IPage<ProjectOv> page1 = pMapper.selectAll(page,queryWrapper);
        PageResult result = new PageResult();
        result.setRows(page1.getRecords());
        result.setTotal(page1.getTotal());
        return result;

    }

    /**
     * 添加项目
     * @param project  封装项目信息
     * @return
     */

    @PostMapping("add")
    public Result addProject(@RequestBody Project project){
        boolean row = pService.save(project);
        Result result  = new Result();
        if (row){
            result.setMessage("success");
            result.setFlag(true);
        }else {
            result.setMessage("fail");
        }
        return result;
    }

    /**
     *根据id查project做数据回显
     * @param pid
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer pid){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",pid);
        Project one = pService.getOne(queryWrapper);
        Result result = new Result();
        if (one != null) {
            result.setMessage("success");
            result.setFlag(true);
            result.setData(one);
            return result;
        }else {
            result.setMessage("fail");
            return result;
        }
    }

    /**
     * 修改项目
     * @param project  封装project信息
     * @return
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody Project project){
        project.setUpdatetime(new Date());
        boolean b = pService.updateById(project);
        Result result = new Result();
        if (b) {
            result.setFlag(true);
            result.setMessage("success");
        }else {
            result.setMessage("fail");
        }
        return result;
    }

    @RequestMapping("delete")
    public Result del(@RequestParam Integer pid){
        Result result = new Result();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        Project project = new Project();
        project.setPid(pid);
        project.setStatus(0);
        boolean b = pService.updateById(project);
        if (b) {
            result=Result.success();
        }else {
            result=Result.fail();
        }
        return result;
    }


}
